package com.zyd.blog.controller;

import com.zyd.blog.business.annotation.BussinessLog;
import com.zyd.blog.business.enums.PlatformEnum;
import com.zyd.blog.homeart.service.*;
import com.zyd.blog.homeart.service.Address.BaService;
import com.zyd.blog.homeart.util.ResponseObject;
import com.zyd.blog.homeart.util.UuidUtil;
import com.zyd.blog.persistence.beans.*;
import com.zyd.blog.persistence.mapper.CenterMapper;
import com.zyd.blog.util.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liusongqi
 * @since 2020-02-10
 */
@RestController
@RequestMapping("/rotationChart")
public class RotationChartController {
    private final Logger logger = LoggerFactory.getLogger(RotationChartController.class);
    @Autowired
    private IRotationChartService iRotationChartService;
    @Autowired
    private IDictionariesService iDictionariesService;

    @Autowired
    private BaService baService;
    @Autowired
    private IPhotoService iPhotoService;
    @Autowired
    private ClassificationService classificationService;

    @Autowired
    private IDesignArticleService designArticleService;
    @Autowired
    private UuidUtil uuidUtil;
    @Autowired
    private ICommentService iCommentService;
    @Autowired
    private ICommentReplyService iCommentReplyService;
    @Autowired
    private CenterMapper centerMapper;

    //首页轮播图
    @PostMapping(value = "/index")
    @BussinessLog(value = "/rotationChart/index 首页轮播图", platform = PlatformEnum.WEB, save = false)
    public ResponseObject index() {
        ResponseObject ob = new ResponseObject();
        List<RotationChart> index;
        try {
            index = iRotationChartService.index();
        } catch (ServiceException e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setMsg("success");
        ob.setCode(0);
        ob.setData(index);
        return ob;
    }

    // 排序  下拉框 内容
    @RequestMapping(value = "/kind", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/kind 分类", platform = PlatformEnum.WEB, save = false)
    public ResponseObject kind(String state) throws Exception {
        ResponseObject ob = new ResponseObject();
        if (state.isEmpty()) {
            ob.setCode(1);
            ob.setMsg("不可为空");
            return ob;
        }
        List<Dictionaries> kind;

        try {
            kind = iDictionariesService.kind(state);
        } catch (Exception e) {
            ob.setMsg(e.getMessage());
            ob.setCode(1);
            return ob;
        }
        ob.setCode(0);
        ob.setData(kind);
        return ob;
    }

    //菜单名字 排序名称
    @RequestMapping(value = "/menu", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/menu 菜单 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject menu(String type) {
        ResponseObject ob = new ResponseObject();
        if (type.isEmpty()) {
            ob.setCode(1);
            ob.setMsg("不可为空");
            return ob;
        }
        List<Classification> menu;
        try {
            menu = classificationService.menu(type);
        } catch (Exception e) {
            ob.setMsg(e.getMessage());
            ob.setCode(1);
            return ob;
        }
        ob.setCode(0);
        ob.setData(menu);
        return ob;
    }

    //家装设计 全部
    @RequestMapping(value = "/soft", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/soft 家装设计 全部 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject soft(String userId,int page,int count) {
        ResponseObject ob = new ResponseObject();
        ob.setCode(0);
        ob.setMsg("success");
        ob.setData(designArticleService.soft(userId,page,count));
        return ob;
    }



    /**
     * 根据规则 变换 获取家装设计
     * region 0省  1 市  2区  3全国  4 全省  5 全市
     * sort    20 时间优先 21 点赞优先  22 热度优先
     * style 只要不为空 直接传过去 就可
     * province 省
     * city  市
     * area  区
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/sort 家装设计 根据规则 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject sort(String region, String sort, String style, String province, String city, String area,String userId,String place,int page,int count) {
        ResponseObject ob = new ResponseObject();

        Map<String,Object> sort1;
        try {
            sort1 = designArticleService.sort(region, sort, style, province, city, area,userId,place,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(sort1);
        return ob;
    }


    /**
     *     根据文章 id  点赞   点赞数量加1
     * state 0收藏 1点赞 2评论 3分享
     */
    @RequestMapping(value = "/fabulous", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/fabulous 进行点赞 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject fabulous(String designId,String userId,String category,String state) {
        ResponseObject ob = new ResponseObject();
        Integer sort1;
        try {
            sort1 = designArticleService.fabulous(designId,userId,category,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(sort1);
        ob.setMsg("success");
        return ob;
    }


    //添加评论
    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/addComment 添加评论 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject addComment(Comment comment) {
        ResponseObject ob = new ResponseObject();
        Integer sort1;
        try {
            sort1 = iCommentService.addComment(comment);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(sort1);
        ob.setMsg("success");
        return ob;
    }


    //最新评论  显示距离现在时间最近的三条评论
    @RequestMapping(value = "/threeComment", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/threeComment 最新三条评论 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject threeComment(String answerId,String state,String userId) {
        ResponseObject ob = new ResponseObject();
        List<CommentNew> sort1;
        try {
            sort1 = iCommentService.threeComment(answerId,state,userId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(sort1);
        ob.setMsg("success");
        return ob;
    }


    //添加文章
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/addArticle 发布软装产品 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject addArticle(DesignArticle designArticle) {
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer = designArticleService.addArticle(designArticle);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }

    /**
     *     根据文章 id  取消点赞   点赞数量减1
     * state 0收藏 1点赞 2评论 3分享
     */
    @RequestMapping(value = "/reduceZan", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/reduceZan 取消点赞 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject reduceZan(String designId,String userId,String category,String state) {
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer = designArticleService.reduceZan(designId,userId,category,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     根据文章 id  获取具体内容
     */
    @RequestMapping(value = "/specific", method = RequestMethod.POST)
    public ResponseObject specific(String designId,String userId,String designStyle,String designPlace) throws Exception {
        logger.info("/rotationChart/specific 获取具体内容, designId={},userId={},designStyle={},designPlace={}", designId, userId, designStyle, designPlace);
        ResponseObject ob = new ResponseObject();
        Map<String,Object> specific;
//        try {
             specific = designArticleService.specific(designId, userId,designStyle,designPlace);
//        } catch (Exception e) {
//            ob.setCode(1);
//            ob.setMsg(e.getMessage());
//            return ob;
//        }
        ob.setCode(0);
        ob.setData(specific);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     ui图 115  全部评论 以及评论回复的 3条
     */
    @RequestMapping(value = "/allComment", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/allComment 全部评论 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject allComment(String answerId,String state,String userId) {
        ResponseObject ob = new ResponseObject();
        Map<String, List> comment;
        try {
             comment = iCommentService.Comment(answerId, state, userId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(comment);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     ui图 116  全部回复 以及评论回复的 3条
     */
    @RequestMapping(value = "/allReply", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/allReply 全部回复 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject allReply(String commentId,String state,String userId) {
        ResponseObject ob = new ResponseObject();
        Map<String, Object> comment;
        try {
            comment = iCommentReplyService.Reply(commentId, state, userId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(comment);
        ob.setMsg("success");
        return ob;
    }



    /**
     *     推荐文章r  style  风格  place 场所
     *     先计算热度，若现在看的场所是别墅，就取别墅热度前10的数据。
     */
    @RequestMapping(value = "/recommendArticle", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/recommendArticle 推荐文章 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject recommendArticle(String style,String place) {
        ResponseObject ob = new ResponseObject();
        List<DesignArticle> designArticles;
        try {
            designArticles = designArticleService.recommendArticle(style, place);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(designArticles);
        ob.setMsg("success");
        return ob;
    }


    @RequestMapping(value = "/insertPhoto", method = RequestMethod.POST)
    public ResponseObject insertreminder(HttpServletRequest request, String userId) throws Exception {
        List<MultipartFile> listFile = null;
        if (request instanceof MultipartHttpServletRequest) {
            listFile = ((MultipartHttpServletRequest) request).getFiles("file");
        }
        ResponseObject ob = new ResponseObject();
        ob.setData(baService.DOMAIN_NAME + iPhotoService.upload(listFile, userId));
        ob.setMsg("请求成功");
        ob.setCode(0);
        return ob;
    }

    /**  此文章为草稿箱  现在想要 编辑 此文章
     *   改变文章的状态  改为正在编辑的状态
     */
    @RequestMapping(value = "/updateDraft", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/updateDraft 改变文章的状态 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject updateDraft(String designId,String userId) {
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer  = designArticleService.updateDraft(designId, userId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }

    /**  此文章为草稿箱  现在想要 编辑 此文章
     *      文章编辑完成想要发布
     */
    @RequestMapping(value = "/editDraft", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/editDraft 文章编辑完成想要发布 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject editDraft(String designId,String userId) {
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer  = designArticleService.updateDraft(designId, userId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }

    /**
     *     我添加的 家装产品
     */
    @RequestMapping(value = "/myAddDesign", method = RequestMethod.POST)
    public ResponseObject myAddFine(String userId,int page,int count)  {
        logger.info("/rotationChart/myAddDesign 我添加的 家装产品, userId={}",  userId);
        ResponseObject ob = new ResponseObject();
        Map<String,Object>  designArticles;
        try {
            designArticles = designArticleService.myAdd(userId,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(designArticles);
        ob.setMsg("success");
        return ob;
    }

    /**
     *     推荐页
     */
    @RequestMapping(value = "/support", method = RequestMethod.POST)
    public ResponseObject index(String userId)  {
        logger.info("/rotationChart/index 推荐页, userId={}",  userId);
        ResponseObject ob = new ResponseObject();
        Map<String,Object> map;
        try {
            map = designArticleService.index(userId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(map);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     草稿箱的  根据id  查看具体内容进行编辑
     */
    @RequestMapping(value = "/drafts/edit", method = RequestMethod.POST)
    public ResponseObject biaji(String designId)  {
        logger.info("/rotationChart/drafts/edit 草稿箱编辑, designId={}",  designId);
        ResponseObject ob = new ResponseObject();
        DesignArticle designArticles;
        try {
            designArticles = designArticleService.bianji(designId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(designArticles);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     删除文章
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseObject delete(String designId,String state)  {
        logger.info("/rotationChart/delete 删除文章, designId={},state={}",  designId,state);
        ResponseObject ob = new ResponseObject();
        Integer designArticles;
        try {
            designArticles = designArticleService.delete(designId,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(designArticles);
        ob.setMsg("success");
        return ob;
    }

    /**
     *     由草稿箱 编辑文章 从新发布
     *
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseObject update(DesignArticle designArticle)  {
        logger.info("/rotationChart/update 编辑文章 从新发布, designArticle={}",  designArticle);
        ResponseObject ob = new ResponseObject();
        Integer designArticles;
        try {
            designArticles = designArticleService.update(designArticle);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(designArticles);
        ob.setMsg("success");
        return ob;
    }


    //个人主页  家装设计  自己看自己
    @RequestMapping(value = "/personalSoft", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/personalSoft 个人主页  家装设计 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject personalSoft(String userId,int page,int count,String style) {
        ResponseObject ob = new ResponseObject();
        ob.setCode(0);
        ob.setMsg("success");
        ob.setData(designArticleService.personalSoft(userId,page,count,style));
        return ob;
    }

    //个人主页  家装设计  别人看你
    @RequestMapping(value = "/personalSoftOrder", method = RequestMethod.POST)
    public ResponseObject personalSoftOrder(String userId,int page,int count,String style,String coverUserId) {
        logger.info("/fineArt/personalSoftOrder 个人主页  家装设计,userId={},page={},count={},coverUserId={}",userId,page,count,coverUserId);
        ResponseObject ob = new ResponseObject();
        ob.setCode(0);
        ob.setMsg("success");
        ob.setData(designArticleService.personalSoftOrder(userId,page,count,style,coverUserId));
        return ob;
    }

    //发布软装产品编辑表
    @RequestMapping(value = "/addArticleEdit", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/addArticleEdit 发布软装产品编辑表 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject addArticle(DesignArticleEdit designArticleEdit) {
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer = designArticleService.addArticleEdit(designArticleEdit);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }

    //结社编辑 之后的 通过了
    @RequestMapping(value = "/tongguole", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/tongguole 发布软装产品编辑表 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject tongguole(String id) {
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer = designArticleService.tongguole(id);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }

    //搜索
    @RequestMapping(value = "/searchDesign", method = RequestMethod.POST)
    @BussinessLog(value = "/rotationChart/searchDesign 搜索 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject searchDesign(String value,String userId, int page, int count) {
        ResponseObject ob = new ResponseObject();
        Map<String,Object> integer;
        try {
            integer = designArticleService.searchDesign(value,userId,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }


    /**
     * @param
     * @return    //点击 换一句
     */
    @RequestMapping(value = "/another", method = RequestMethod.POST)
    public ResponseObject another() {
        logger.info("/rotationChart/another 点击 换一句");
        ResponseObject ob = new ResponseObject();
        List<Interest> listall;
        listall = designArticleService.interestAll();
        List<Interest> newList = new ArrayList<>();
        for (int i = 3; i >=1; i--) {
            Random random = new Random();
            Math.random();
            //在数组大小之间产生一个随机数 j
            int j = random.nextInt(listall.size());
            //取得list 中下标为j 的数据存储到 listRandom 中
            newList.add(listall.get(j));
            //把已取到的数据移除,避免下次再次取到出现重复
            listall.remove(j);
        }
        ob.setData(newList);
        ob.setCode(0);
        ob.setMsg("请求成功");
        return ob;

    }


    /**
     * @param
     * @return    禁用用户
     */
    @RequestMapping(value = "/stopUser", method = RequestMethod.POST)
    public ResponseObject stopUser(String userId) {
        logger.info("/rotationChart/stopUser 禁用用户 userId={}",userId);
        ResponseObject ob = new ResponseObject();
        Integer listall;
        listall = designArticleService.stopUser(userId);
        ob.setData(listall);
        ob.setCode(0);
        ob.setMsg("请求成功");
        return ob;

    }

    /**
     * @param
     * @return    开启用户
     */
    @RequestMapping(value = "/passUser", method = RequestMethod.POST)
    public ResponseObject passUser(String userId) {
        logger.info("/rotationChart/passUser 开启用户 userId={}",userId);
        ResponseObject ob = new ResponseObject();
        Integer listall;
        listall = designArticleService.passUser(userId);
        ob.setData(listall);
        ob.setCode(0);
        ob.setMsg("请求成功");
        return ob;

    }
}
